package cc.comac.mom.controller;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.QualityAsrCheckFormService;
import cc.comac.mom.bean.QualityAsrCheckForm;
import cc.comac.mom.bean.QualityReject;
import cc.comac.mom.entity.AsrCheckFormPrint;
import cc.comac.mom.util.BaseConnectionData;
import cc.comac.mom.util.BeanManager;
import cc.comac.mom.util.JasperHelper;
import cc.comac.mom.util.ResultBean;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/QualityAsrCheckForm")
public class QualityAsrCheckFormController {

	@Autowired
	private QualityAsrCheckFormService qualityAsrCheckFormService;
	
	@Autowired
	private BaseConnectionData baseConnectionData;

	// 保存修改按钮触发，保存表单数据以及对表单详情数据的增删改操作
	@ApiOperation("保存修改按钮触发，保存表单数据以及对表单详情数据的增删改操作")
	@RequestMapping(value = "/batchUpdateAsrCheckFormAndDetail", method = RequestMethod.POST)
	public ResultBean batchUpdateAsrCheckFormAndDetail(@RequestBody QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean batchUpdateAsrCheckFormAndDetail = qualityAsrCheckFormService
				.batchUpdateAsrCheckFormAndDetail(qualityAsrCheckForm);
		return batchUpdateAsrCheckFormAndDetail;
	}

	@ApiOperation("提交检验按钮")
	@RequestMapping(value = "/submitCheck", method = RequestMethod.POST)
	public ResultBean submitCheck(@RequestBody QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean submitCheck = qualityAsrCheckFormService.submitCheck(qualityAsrCheckForm);
		return submitCheck;
	}

	@ApiOperation("提交审核")
	@RequestMapping(value = "/submitReview", method = RequestMethod.POST)
	public ResultBean submitReview(@RequestBody QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean submitReview = qualityAsrCheckFormService.submitReview(qualityAsrCheckForm);
		return submitReview;
	}
	
	@ApiOperation("归档")
	@RequestMapping(value = "/submitToFile", method = RequestMethod.POST)
	public ResultBean submitToFile(@RequestBody QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean submitToFile = null;
		try {
			submitToFile = qualityAsrCheckFormService.submitToFile(qualityAsrCheckForm);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return submitToFile;
	}

	@ApiOperation("驳回按钮")
	@RequestMapping(value = "/submitReject", method = RequestMethod.POST)
	public ResultBean submitReject(@RequestBody QualityReject QualityReject) {
		ResultBean submitReject = qualityAsrCheckFormService.submitReject(QualityReject);
		return submitReject;
	}

	@ApiOperation("批量新增检验表单数据")
	@RequestMapping(value = "/batchInsertAsrCheckForm", method = RequestMethod.POST)
	public ResultBean batchInsertAsrCheckForm(@RequestBody List<QualityAsrCheckForm> list) {
		ResultBean batchInsertAsrCheckForm = qualityAsrCheckFormService.batchInsertAsrCheckForm(list);
		return batchInsertAsrCheckForm;
	}

	@ApiOperation("查询检验表单及检验表单详情信息")
	@RequestMapping(value = "/selectBySomefields", method = RequestMethod.POST)
	public ResultBean selectBySomefields(@RequestBody QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean selectBySomefields = qualityAsrCheckFormService.selectBySomefields(qualityAsrCheckForm);
		return selectBySomefields;
	}
	
	@ApiOperation("通过检验表单主键查询打印检验表单数据")
	@RequestMapping(value = "/printByCheckformId/{formId}", method = RequestMethod.GET)
	public ResultBean printByCheckformId(@PathVariable("formId") long formId,HttpServletRequest request,HttpServletResponse response) {
		ResultBean resultBean = new ResultBean(false);
		
		try {
			AsrCheckFormPrint printByCheckformId = qualityAsrCheckFormService.printByCheckformId(formId);
			Map<String, Object> beanToMap = BeanManager.beanToMap(printByCheckformId);
			//指定导出文件名称
	        String exportFilePath="ndtTestInvoice";
	        //指定模板文件
	        ClassPathResource classPathResource = new ClassPathResource("config/asrCheckForm.jasper");
			Connection connection = baseConnectionData.getConnection();
	        File file=classPathResource.getFile();
	        JasperHelper.showPdf(exportFilePath, file.getPath(), request, response, beanToMap, connection);
		} catch (Exception e) {
			resultBean.setErrorMessge(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
}
